// Die
// v1.0

// Includes

#include <windows.h>
#include <gl/gl.h>
#include "hmi.h"
// #include "hmi_gl_wafer.h"

#define DIE_NUMBER 55
#define DIE_SIZE WAFER_SIZE*0.1f

// *****************************************************************************
// *****************************************************************************
// Methodes/Modules
// *****************************************************************************
// *****************************************************************************

Die Die;

// *****************************************************************************
// *****************************************************************************
// Functions
// *****************************************************************************
// *****************************************************************************
      

// *****************************************************************************
// HMI_GL_die_pos_set_X
// *****************************************************************************

void HMI_GL_die_pos_set_X(int i, double x){
Die.setPosX(i,x);     
} // HMI_GL_die_pos_set_X

void HMI_GL_die_pos_set_Y(int i, double y){
Die.setPosY(i,y);     
} // HMI_GL_die_pos_set_Y

double HMI_GL_die_pos_get_X(int i){
return Die.getPosX(i);     
} // HMI_GL_die_pos_set_X

double HMI_GL_die_pos_get_Y(int i){
return Die.getPosY(i);     
} // HMI_GL_die_pos_set_X

      
// *****************************************************************************
// HMI_GL_die_draw_standard
// *****************************************************************************

void HMI_GL_die_draw_standard(){

  HMI_GL_color(COLOR_YLW);

// draw die standard
  {
  double wafer_size=0.65f;
  double saw_size_factor=0.90f;
  int number_dies=DIE_NUMBER;
  double size_die;
  double step_die;
  double offsetX=0.00f;
  double offsetY=0.00f;
  double rW;          // radius for rotation of vertex from W0
  double rD;          // radius for rotation of die from W0



  size_die=(wafer_size /(double) number_dies * 2)* saw_size_factor;
  step_die=size_die/saw_size_factor;

  for (int i=-1;i<=2;i+=2){
  for (int j=-1;j<=2;j+=2){
  for (double y=0.0f;y<=wafer_size;y=y+step_die){
  for (double x=0.0f;x<=wafer_size;x=x+step_die){

  rW=sqrt(x*x+y*y); // calculate radius for rotation
  rD=sqrt(size_die*size_die+size_die*size_die); // calculate radius for rotation
  //set color of die
  if (rW+rD/2<WAFER_SIZE) HMI_GL_color(COLOR_DIE_IN);
  else HMI_GL_color(COLOR_DIE_OUT);
  HMI_GL_vertex_rect(x*i+offsetX-size_die/2,y*j+offsetY-size_die/2,x*i+offsetX+size_die/2,y*j+offsetY+size_die/2);
  }}}}}

} // HMI_GL_die_draw_standard

// *****************************************************************************
// HMI_GL_die_init
// *****************************************************************************

void HMI_GL_die_init1(){

// draw die standard
  double wafer_size=0.40f;
  double saw_size_factor=0.90f;
  int number_dies=DIE_NUMBER;
  int id=0;
  double size_die;
  double step_die;
  double offsetX=0.00f;
  double offsetY=0.00f;
  double rW;          // radius for rotation of vertex from W0
  double rD;          // radius for rotation of die from W0

  struct {
    int id;
    int code;
    double x;
    double y;
  }die[DIE_NUMBER_MAX];

  size_die=(wafer_size /(double) number_dies * 2)* saw_size_factor;
  step_die=size_die/saw_size_factor;
  Die.setSize(size_die);
  
  /*for (int i=-1;i<=2;i+=2){
  for (int j=-1;j<=2;j+=2){
  for (double y=0.0f;y<=wafer_size;y=y+step_die){
  for (double x=0.0f;x<=wafer_size;x=x+step_die){
  die[id].id=id++;
  die[id].x=x*i;
  die[id].y=y*j;*/

  /*{
  for (double y=wafer_size;y>=-wafer_size;y=y-step_die){
  for (double x=-wafer_size;x<=wafer_size;x=x+step_die){

  rW=sqrt(x*x+y*y);                             // calculate radius for rotation
  rD=sqrt(size_die*size_die+size_die*size_die); // calculate radius of die

  //set code of die
  if (rW+rD/2<WAFER_SIZE){ 
   id++;
   HMI_GL_die_pos_set_X(id,x);
   HMI_GL_die_pos_set_Y(id,y);
   Die.setCode(id, COLOR_DIE_IN);
  }
  //else die[id].code=COLOR_DIE_OUT;
  }}
  Die.setNumberOfDies(id);
  }*/

  {
  for (int i=0;i<=number_dies*number_dies;i++){
  int row,col;
  bool dir;
  double x,y;
  double x0=-0.5f,y0=0.5f;
  
  if (!dir) col++;
  if ( dir) col--;
  if (col == number_dies) {dir = true; row--;}
  if (col == 0) {dir = false; row--;}
    
  x=x0+col*step_die;
  y=y0+row*step_die;
  
  rW=sqrt(x*x+y*y);                             // calculate radius for rotation
  rD=sqrt(size_die*size_die+size_die*size_die); // calculate radius of die

  //set code of die
//  if (rW+rD/2<WAFER_SIZE){ 
  if (rW+rD/2<WAFER_SIZE){ 
   HMI_GL_die_pos_set_X(i,x);
   HMI_GL_die_pos_set_Y(i,y);
   Die.setCode(i, COLOR_RED);
   }
  //else {Die.setCode(i, COLOR_BLU);}
  
  Die.setNumberOfDies(number_dies*number_dies);
  }
  }


   

} // HMI_GL_die_init

// *****************************************************************************
// HMI_GL_die_init
// *****************************************************************************

void HMI_GL_die_init2(){

  int number_dies_row=10;
  int number_dies;
  int id2;
  double wafer_size=WAFER_SIZE;
  double saw_size_factor=0.90f;
  double size_die;
  double step_die;
  double rW;          // radius for rotation of vertex from W0
  double rD;          // radius for rotation of die from W0

// draw die standard
  number_dies=100;
  size_die=(wafer_size /(double) number_dies_row * 2)* saw_size_factor;
  step_die=size_die/saw_size_factor;
  Die.setSize(size_die);

  for (int i=0;i<=number_dies;i++){
  int row,col;
  bool dir;
  double x,y;
  
  if (!dir) col++;
  if ( dir) col--;
  if (col == number_dies_row) {dir = true; row--;}
  if (col == 0) {dir = false; row--;}
    
  x=col*step_die-WAFER_SIZE;
  y=row*step_die+WAFER_SIZE;
  
  rW=sqrt(x*x+y*y);                             // calculate radius for rotation
  rD=sqrt(size_die*size_die+size_die*size_die); // calculate radius of die

  Die.setCode(i, COLOR_GRY);

  //set code of die
//  if (rW+rD/2<WAFER_SIZE){ 
  if (col>1 && col<number_dies_row){ 
   HMI_GL_die_pos_set_X(i,x);
   HMI_GL_die_pos_set_Y(i,y);
   Die.setCode(i, COLOR_WHT);
   }
  //set code of die

//  if (rW+rD/2<WAFER_SIZE){ 
  if (rW+rD/2<WAFER_SIZE){ 
   id2++;
   Die.setCode(i, COLOR_RED);
   }
  //else {Die.setCode(i, COLOR_BLU);}
  
  Die.setNumberOfDies(number_dies);
  } // end for


   

} // HMI_GL_die_init

// *****************************************************************************
// HHMI_GL_die_draw
// *****************************************************************************

void HMI_GL_die_draw_all(){
  // draw die
  for (int i=1;i<=Die.getNumberOfDies();i+=1){
  HMI_GL_die_draw(i,Die.code(i));
  }
} // HMI_GL_die_draw

// *****************************************************************************
// HHMI_GL_die_draw
// *****************************************************************************

void HMI_GL_die_draw(int i, int code){
  double x1,y1,x2,y2;
  x1=Die.getPosX(i)-Die.sizeX()/2;
  y1=Die.getPosY(i)-Die.sizeY()/2;
  x2=x1+Die.sizeX();
  y2=y1+Die.sizeY();
  HMI_GL_color(code);
  HMI_GL_vertex_rect(x1,y1,x2,y2);
} // HMI_GL_die_draw

// *****************************************************************************
// HHMI_GL_die_draw
// *****************************************************************************

void HMI_GL_die_set_code(int i, int code){
  Die.setCode(i, code);
} // HMI_GL_die_draw

